package com.bosui.ycweb;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {
    AnimUtil animUtil;
    private float bgAlpha = 1f;
    private boolean bright = false;
    ImageView logo;
    ImageView hello;

    private String TAG = "SplashActivity:";

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            hello.setVisibility(View.GONE);
            logo.setVisibility(View.VISIBLE);
            logo.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000 * 5).withEndAction(() -> {
                toggleBright();
            });
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        logo = findViewById(R.id.logo);
        hello = findViewById(R.id.hello);
        animUtil = new AnimUtil();
        //            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        //            overridePendingTransition
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(1);
            }
        }, 2000);

    }

    private void toggleBright() {
        //三个参数分别为： 起始值 结束值 时长 那么整个动画回调过来的值就是从0.5f--1f的
        animUtil.setValueAnimator(0.4f, 1f, 2000);
        animUtil.addUpdateListener(new AnimUtil.UpdateListener() {
            @Override
            public void progress(float progress) {
                //此处系统会根据上述三个值，计算每次回调的值是多少，我们根据这个值来改变透明度
                Log.d(TAG, "progress: " + progress);
                bgAlpha = bright ? progress : (1.4f - progress);//三目运算，应该挺好懂的。
                Log.d(TAG, "bgAlpha: " + bgAlpha);
                backgroundAlpha(bgAlpha);//在此处改变背景，这样就不用通过Handler去刷新了。
            }
        });
        animUtil.addEndListner(animator -> {
            //在一次动画结束的时候，翻转状态
            bright = !bright;
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        });
        animUtil.startAnimator();
    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }

    /***
     * 此方法用于改变背景的透明度，从而达到“变暗”的效果
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }
}